package code16_thread;

public class Test03 {

	public static void main(String[] args) {
//		MyWindow m1 = new MyWindow("窗口1");
//		MyWindow m2 = new MyWindow("窗口2");
//		m1.start();
//		m2.start();
		// 各自卖了100，共200
		
		MyTicket mt = new MyTicket();
		Thread t1 = new Thread(mt, "窗口1");
		Thread t2 = new Thread(mt, "窗口2");
		t1.start();
		t2.start();
		// 共同卖了 100
	}
	
}
class MyWindow extends Thread {
	int num = 100;// 票
	public MyWindow(String name) {
		super(name);
	}
	@Override
	public void run() {
		while (num > 0) {
			System.out.println(getName() + " 卖票: " + num);
			num--;
		}
	}
}

class MyTicket implements Runnable {
	int num = 100;// 票
	@Override
	public void run() {
		while (true) {
			Thread t = Thread.currentThread();
			// 2, 1 --> 0
			System.out.println(t.getName() + " 卖票: " + num);
			// 1, 1
			// 2, 0
			
			
			num--;
			// 1, 0
			// 2, -1
			if (num == 0) {
				break;
			}
//			if (num < 0) {
//				break;
//			}
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}